package com.example.shushiyunsst.fragment.main.home.child

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import com.alibaba.fastjson.JSON
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.QMUIDisplayHelper
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.constant.CURRENT_DEVICE_TOKEN
import com.example.shushiyunsst.fragment.main.home.child.bean.IconNodeIncomeListBean
import com.example.shushiyunsst.fragment.main.home.child.miner22.bean.SubAccountListBean
import com.example.wallet.db.UserInfoManager
import com.example.wallet.utils.Preference
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

class IconNodeIncomeListFragment : SimpleListFragment<IconNodeIncomeListBean>() {


    companion object {
        fun newInstance(): IconNodeIncomeListFragment {
            val args = Bundle()
            val fragment = IconNodeIncomeListFragment()
            fragment.arguments = args
            return fragment
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isAddDividerItemDecoration=false
        isEnableLoadMore=false
    }

    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundResource(R.color.bg_color)
        addHeader(R.layout.comment_header_layout)
        mHeader.setBackgroundResource(R.color.bg_color_0)
        val mToolbar: Toolbar = mHeader.findViewById(R.id.mToolbar)
        mToolbar.setTitle(R.string.icon_node_income)
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }
        mRecyclerView.clipToPadding=false
        mRecyclerView.setPadding(0,0,0,QMUIDisplayHelper.dp2px(_mActivity,10))
    }

    override fun getItemLayout(): Int {
        return R.layout.icon_node_income_list_fragment
    }

    override fun convertItem(helper: JssBaseViewHolder?, item: IconNodeIncomeListBean?) {
        helper?.setText(R.id.desc,item?.typeDesc)
            ?.setText(R.id.numTv, "${item?.sum}")
            ?.setText(R.id.timeTv, "${item?.createDate}")
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(IconNodeIncomeListBean::class.java).endSubType().build()
    }

    override fun netRequest() {
        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo
        val deviceTokens = Preference.HOLDER.getCustomAppProfile(CURRENT_DEVICE_TOKEN)
        RestClient.builder()
            .url(R.string.incomeLog)
            .raw(
                JSON.toJSONString(
                    mapOf(
                        "uid" to userInfo?.uid,
                        "deviceToken" to deviceTokens
                    )
                )
            )
            .success {
                parseDate(it)
            }
            .error { _, _ ->
                 onFailed()
            }.failure {
                onFailed()
            }.build().post()
    }

    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val obj = JSON.parseObject(data)
        val incomeLog = obj["incomeLog"].toString()
        val array = JSON.parseArray(incomeLog, IconNodeIncomeListBean::class.java)
        loadListDate(array)
    }
}